package ui.activity.wallet.presenter;


import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import base.BaseBiz;
import coom.Constants;
import ui.activity.wallet.biz.RewardsDetailsBiz;
import ui.activity.wallet.contract.RewardsDetailsContract;
import ui.model.ReardsGrowthBean;
import ui.model.RewardsChallengeBean;
import ui.model.RewardsDetailsBean;
import ui.model.RewardsListBean;

/**
 * Created by chenjian on 19/2/26
 */

public class RewardsDetailsPresenter implements RewardsDetailsContract.Presenter {

    RewardsDetailsContract.View view;
    RewardsDetailsBiz biz;
    List<RewardsDetailsBean> list = new ArrayList<>();
    List<ReardsGrowthBean.RecordsBean> list1 = new ArrayList<>();
    List<RewardsChallengeBean.RecordsBean> list2 = new ArrayList<>();
    private int pageIndex = 1;

    @Inject
    public RewardsDetailsPresenter(RewardsDetailsContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        this.biz = (RewardsDetailsBiz) biz;
    }


    @Override
    public void getListInfo(final boolean isRefresh, int type, long time, int year, int month) {
        if (type == 5) {
            if (isRefresh) {
                list1.clear();
                pageIndex = 1;
            }
            view.loading(true);
            biz.getGrowthListInfo(pageIndex, Constants.PAGESIZE, year, month, new BaseBiz.Callback<ReardsGrowthBean>() {
                @Override
                public void onSuccess(ReardsGrowthBean info) {
                    list1.addAll(info.getRecords());
                    view.upDateUi(info.getRecords());
                    pageIndex++;
                    view.hasLoadMore(Constants.PAGESIZE == info.getRecords().size());
                    if (isRefresh) {
                        view.refreshComplete();
                    } else {
                        view.loadMoreComplete();
                    }
                    view.loading(false);
                }

                @Override
                public void onFailure(ReardsGrowthBean info) {
                    view.loading(false);
                    if (isRefresh) {
                        view.refreshComplete();
                    } else {
                        view.loadMoreComplete();
                    }
                }
            });
        } else if (type == 6) {
            if (isRefresh) {
                list2.clear();
                pageIndex = 1;
            }
            view.loading(true);
            biz.getChanllengeListInfo(pageIndex, Constants.PAGESIZE, year, month, new BaseBiz.Callback<RewardsChallengeBean>() {
                @Override
                public void onSuccess(RewardsChallengeBean info) {
                    list2.addAll(info.getRecords());
                    view.upDateUIinfo(list2);
                    pageIndex++;
                    if (isRefresh) {
                        view.refreshComplete();
                    } else {
                        view.loadMoreComplete();
                    }
                    view.hasLoadMore(Constants.PAGESIZE == info.getRecords().size());
                    view.loading(false);
                }

                @Override
                public void onFailure(RewardsChallengeBean info) {
                    view.loading(false);
                    if (isRefresh) {
                        view.refreshComplete();
                    } else {
                        view.loadMoreComplete();
                    }
                }
            });
        } else {
            if (isRefresh) {
                list.clear();
                pageIndex = 1;
            }
            view.loading(true);
            biz.getListInfo(type, pageIndex, Constants.PAGESIZE, year, month, new BaseBiz.Callback<RewardsListBean>() {
                @Override
                public void onSuccess(RewardsListBean info) {
                    list.addAll(info.getRecords());
                    view.upDateUI(list);
                    pageIndex++;
                    if (isRefresh) {
                        view.refreshComplete();
                    } else {
                        view.loadMoreComplete();
                    }
                    view.hasLoadMore(Constants.PAGESIZE == info.getRecords().size());
                    view.loading(false);
                }

                @Override
                public void onFailure(RewardsListBean info) {
                    view.loading(false);
                    if (isRefresh) {
                        view.refreshComplete();
                    } else {
                        view.loadMoreComplete();
                    }
                }
            });
        }
    }

}
